package com.wef.demo.service.impl;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.Hibernate;

import com.gfa4j.hibernate.BaseObject;
import com.gfa4j.service.impl.BaseServiceImpl;
import com.wef.demo.entity.Clazz;
import com.wef.demo.entity.Student;
import com.wef.demo.entity.StudentSubject;
import com.wef.demo.entity.domain.StudentDomain;
import com.wef.demo.entity.domain.StudentSubjectDomain;
import com.wef.demo.service.StudentService;
import com.wef.demo.service.StudentSubjectService;

public class StudentServiceImpl extends BaseServiceImpl implements StudentService {

	private StudentSubjectService studentSubjectService;

	public void setStudentSubjectService(StudentSubjectService studentSubjectService) {
		this.studentSubjectService = studentSubjectService;
	}

	public Student loadStudentById(String studentId) {
		return (Student) super.loadByPK(Student.class, StudentDomain.studentId, studentId);
	}

	public Student addStudent(Student student) {
		student = (Student) super.saveObject(student);
		if (Hibernate.isPropertyInitialized(student, StudentDomain.studentSubjects) && student.getStudentSubjects() != null && student.getStudentSubjects().size() > 0) {
			for (StudentSubject studentSubject : student.getStudentSubjects()) {
				studentSubject.setRowStatus(BaseObject.ROWSTATUS_ADDED);
				studentSubject.setStudent(student);
			}
			studentSubjectService.saveOrUpdateStudentSubjects(student.getStudentSubjects());
		}
		return student;
	}

	public Student updateStudent(Student student) {
		if (Hibernate.isPropertyInitialized(student, StudentDomain.studentSubjects) && student.getStudentSubjects() != null && student.getStudentSubjects().size() > 0) {
			studentSubjectService.saveOrUpdateStudentSubjects(student.getStudentSubjects());
		}
		return (Student) super.updateObject(student);
	}

	public void removeStudent(Student student) {
		// 在lazyLoading为false的情况下重新载入子表数据
		List<StudentSubject> deleteStudentSubjects = super.findByProperty(StudentSubject.class, StudentSubjectDomain.student, student);
		// 执行子表的删除动作
		if (deleteStudentSubjects != null && deleteStudentSubjects.size() > 0) {// 判断
			for (StudentSubject aStudentSubject : deleteStudentSubjects) {
				super.evict(aStudentSubject.getStudent()); // 剔除被重复载入的主表对象
				studentSubjectService.removeStudentSubject(aStudentSubject);
			}
		}
		this.deleteObject(student);
	}

	public Student saveOrUpdateStudent(Student student) {
		if (student.getRowStatus() == BaseObject.ROWSTATUS_ADDED) {
			return this.addStudent(student);
		}
		if (student.getRowStatus() == BaseObject.ROWSTATUS_MODIFIED) {
			return this.updateStudent(student);
		}
		if (student.getRowStatus() == BaseObject.ROWSTATUS_DELETED) {
			this.removeStudent(student);
		}
		return student;
	}

	public Collection<Student> saveOrUpdateStudents(Set<Student> entitys) {
		Set<Student> retStudents = new HashSet<Student>();
		for (Student entity : entitys) {
			retStudents.add(saveOrUpdateStudent(entity));
		}
		return retStudents;
	}
	
	public Student saveOrUpdateStudent2(Student student,String arg,List<String> argLists,boolean bool,Set<String> argSets,Clazz clazz) {
		
		//自定义查询多参数传入
		System.out.println(arg);
		for (Iterator<String> iterator = argLists.iterator(); iterator.hasNext();) {
			System.out.println(iterator.next().toString());
		}
		System.out.println(bool);
		for (Iterator<String> iterator = argSets.iterator(); iterator.hasNext();) {
			System.out.println(iterator.next().toString());
		}
		System.out.println(clazz.getClazzId());
		
		if (student.getRowStatus() == BaseObject.ROWSTATUS_ADDED) {
			return this.addStudent(student);
		}
		if (student.getRowStatus() == BaseObject.ROWSTATUS_MODIFIED) {
			return this.updateStudent(student);
		}
		if (student.getRowStatus() == BaseObject.ROWSTATUS_DELETED) {
			this.removeStudent(student);
		}
		return student;
	}
}